import {
  actionTypes
} from "@/store/actionTypes"
import {
  http,
  urls,
} from '@/utils/index'

export const getSearchList = async (isServer) => {
  const res = await http.get(urls.mbiType, null, isServer)
  const jsonData = await res.data
  return jsonData
}
// 修改参数
export const commonChangeWay = (payload) => async dispatch => {
  return dispatch({
    type: actionTypes.COMMON_CHANGE_WAY,
    payload
  });
}
// mbi菜单列表
export const mbiType = (data) => async dispatch => {
  const res = await http.get(urls.mbiType, {...data}, data.isServer);
  return dispatch({
    type: actionTypes.MBI_TYPE,
    mbiType: res.data.list
  });
};
export const mbiNavigation = (data) => async dispatch => {
  const res = await http.get(urls.mbiNavigation, {...data}, data.isServer);
  return dispatch({
    type: actionTypes.MBI_NAVIGATION,
    mbiNavigation: res.data.list
  });
}
// 4A景区细分类型
export const scenicTree = (isServer) => async dispatch => {
  const res = await http.get(urls.scenicTree, null, isServer);
  return dispatch({
    type: actionTypes.SCENIC_TREE,
    scenicTree: res.data
  });
}
// 最新榜单时间
export const brandTypeDate = (isServer, type) => async dispatch => {
  const res = await http.get(urls.brandTypeDate, {
    type
  }, isServer);
  return dispatch({
    type: actionTypes.BRAND_TYPE_DATE,
    brandTypeDate: res.data
  });
}
// 文旅最新榜单时间
export const tourismDate = (data) => async dispatch => {
  const res = await http.get(urls.getTourismDate, null, data.isServer);
  if (res.data) {
    var d = res.data.split("-")
    res.data = {
      year: d[0],
      month: d[1]
    }
  } else {
    res.data = {
      year: '',
      month: ''
    }
  }
  return dispatch({
    type: actionTypes.BRAND_TYPE_DATE,
    brandTypeDate: res.data
  });
}
// Mbi mci修改时间
export const pageTimeChange = (date) => dispatch => {
  return dispatch({
    type: actionTypes.PAGE_TIME_CHANGE,
    pageTime: date
  });
}
// mci 榜单
export const mciCityRanking = (data) => async dispatch => {
  const res = await http.get(urls.mciMonthRanking, {
    ...data
  }, data.isServer);
  return dispatch({
    type: actionTypes.MCI_CITY_RANKING,
    monthRanking: res.data
  })
}
// 榜单排名
export const monthRanking = (data) => async dispatch => {
  const res = await http.get(urls.monthRanking, {...data}, data.isServer);
  return dispatch({
    type: actionTypes.MONTH_RANKING,
    monthRanking: res.data
  });
}
export const tourismMonthRanking = (data) => async dispatch => {
  const res = await http.get(urls.tourismMonthRanking, {...data}, data.isServer);
  let resData = null
  if (res.status === 200) {
    let d = res.data;
    let list = [];
    if (d.rows.length > 0) {
      d.rows.map(function(item) {
        let i = {...item};
        i.name = item.baseMonitorMainName;
        i.indexData = item.fdFindexValue;
        list.push(i)
      })
      resData = {
        page: {
          currentPage: d.currentPage,
          currentResult: d.currentResult,
          entityOrField: d.entityOrField,
          showCount: d.showCount,
          sortName: d.sortName,
          sortOrder: d.sortOrder,
          totalPage: d.totalPage,
          totalResult: d.totalResult
        },
        list
      }
    } 
  }
  return dispatch({
    type: actionTypes.MONTH_RANKING,
    monthRanking: resData
  });
}
// mbi修改类型
export const changeMbiType = (payload) => dispatch => {
  return dispatch({
    type: actionTypes.CHANGE_MBI_TYPE,
    payload
  });
}
// mci城市 cityList
export const cityList = (isServer) => async dispatch => {
  const res = await http.get(urls.cityList, null, isServer);
  return dispatch({
    type: actionTypes.CITY_LIST,
    cityList: res.data
  });
}
// mci榜单时间
export const latesMciDate = (isServer) => async dispatch => {
  const res = await http.get(urls.latestMciDate, null, isServer);
  return dispatch({
    type: actionTypes.LATEST_MCI_DATE,
    latesMciDate: res.data
  })
}
// 城市cityId 修改
export const changeCityId = (cityId) => async dispatch => {
  return dispatch({
    type: actionTypes.CITY_ID_CHANGE,
    cityId
  });
}
// 获取评论
export const userComment = (data) => async dispatch => {
  const res = await http.get(urls.userComment, {
    ...data
  }, data.isServer);
  let obj = {
    totalPage: 0,
    rows: []
  }
  return dispatch({
    type: actionTypes.USER_COMMENT,
    userComment: res.data === null ? obj : res.data
  })
}
// 今日热词
export const hotwordsToday = (isServer) => async dispatch => {
  const res = await http.get(urls.hotwordsToday, null, isServer)
  return dispatch({
    type: actionTypes.HOTWORDS_TODAY,
    hotWords: res.data
  })
}
// 上升最快的品牌
export const fastestBrand = (isServer) => async dispatch => {
  const res = await http.get(urls.fastestBrand, null, isServer)
  return dispatch({
    type: actionTypes.FASTEST_BRAND,
    fastestBrand: res.data
  })
}
// mbi品牌详情
export const mbiInfoById = (data) => async dispatch => {
  const url = data.theIndex ==='1102' ? urls.MCIInfoById : urls.MBIInfoById
  const res = await http.get(url, data, data.isServer);
  res.data.indexType === 0 && (res.data.indexType = 1102);
  return dispatch({
    type: actionTypes.MBI_INFO_BYID,
    mbiInfoById: res.data
  })
}
export const mciInfoById = (data) => async dispatch => {
  const res = await http.get(urls.MCIInfoById, data, data.isServer);
  return dispatch({
    type: actionTypes.MBI_INFO_BYID,
    mbiInfoById: res.data
  })
}
// mbi mci 指数对比 
export const mbiIndex = (data) => async dispatch => {
  try{
    const res = await http.get(urls.mbiIndex, data, data.isServer);
    return dispatch({
      type: actionTypes.MBI_INDEX,
      mbiIndex: res.data
    })
  }catch(err) {
    return dispatch({
      type: actionTypes.MBI_INDEX,
      mbiIndex: []
    })
  }
  
}
// mbi 排名趋势
export const getHistoryRanking = (data) => async dispatch => {
  const res = await http.get(urls.historyRanking, data, data.isServer);
  return dispatch({
    type: actionTypes.HISTORY_RANKING,
    historyRanking: res.data
  })
}
// 文旅 排名 mbi指数趋势
export const getTourismHistoryRanking = (data) => async dispatch => {
  const res = await http.get(urls.tourismHistoryRanking, data, data.isServer);
  return dispatch({
    type: actionTypes.HISTORY_RANKING,
    historyRanking: res.data
  })
}
// mci 排名趋势
export const getMCIHistoryRankingInfo = (data) => async dispatch => {
  const res = await http.get(urls.getMCIHistoryRankingInfo, data, data.isServer);
  return dispatch({
    type: actionTypes.HISTORY_RANKING,
    historyRanking: res.data
  })
}
// mbi 获取多榜单排名
export const getHistoryRankingInfo = (data) => async dispatch => {
  const res = await http.get(urls.getHistoryRankingInfo, data, data.isServer);
  return dispatch({
    type: actionTypes.HISTORY_RANKING,
    historyRanking: res.data
  })
}
// mbi pk
export const mbiPkData = (data) => async dispatch => {
  const res = await http.get(urls.mbiPkData, data, data.isServer)
  return dispatch({
    type: actionTypes.MBI_PK_DATA,
    mbiPkData: res.data
  })
}
// 媒体报道占比
export const meadiaDataScale = (data) => async dispatch => {
  const res = await http.get(urls.meadiaDataScale, data, data.isServer)
  return dispatch({
    type: actionTypes.MEADIA_DATA_SCALE,
    meadiaDataScale: res.data
  })
}
export const hotwordsBrandId = (data) => async dispatch => {
  const res = await http.get(`${urls.hotwordsBrandId}/${data.brandId}/${data.time}`, null, data.isServer)
  return dispatch({
    type: actionTypes.HOTWORDS_BRANDID,
    hotwordsBrandId: res.data
  })
}
// 媒体报导
export const getMeadiaUpDate = (data) => async dispatch => {
  const res = await http.get(urls.meadiaUpDate, data, data.isServer)
  return dispatch({
    type: actionTypes.MEADIA_UP_DATE,
    meadiaUpDate: res.data
  })
}
// 用户点评
export const commentTrend = (data) => async dispatch => {
  const res = await http.get(urls.commentTrend, data, data.isServer)
  return dispatch({
    type: actionTypes.COMMENT_TREND,
    commentTrend: res.data
  })
}
// 品牌酒店查询
export const searchName = (data) => async dispatch => {
  const res = await http.get(urls.searchName, data, data.isServer)
  return dispatch({
    type: actionTypes.SEARCH_NAME,
    searchData: res.data
  })
}
// 查询品牌点赞数
export const thumbsShow = (data) => async dispatch => {
  const res = await http.get(urls.thumbsShow, data, data.isServer)
  return dispatch({
    type: actionTypes.THUMBS_SHOW,
    searchData: res.data
  })
}
// 情感pk
export const pkComment = (data) => async dispatch => {
  const res = await http.get(urls.pkCommentNew, data, data.isServer)
  // const aj = [{"name":"1洲际","goodPercent":1,"middlePercent":0,"badPercent":0},{"name":"2万豪","goodPercent":1,"middlePercent":0,"badPercent":0}];
  return dispatch({
    type: actionTypes.PK_COMMENT,
    pkCommentData: res.data
    // pkCommentData: aj
  })
}
// 媒体报道量对比 MEADIA_DATA_TREND
export const meadiaDataTrend = (data) => async dispatch => {
  const res = await http.get(urls.meadiaDataTrend, data, data.isServer)
  return dispatch({
    type: actionTypes.MEADIA_DATA_TREND,
    meadiaDataTrend: res.data
  })
}
// 获取关注列表 ATTENTION_LIST
export const attentionList = (data) => async dispatch => {
  const res = await http.get(urls.attentionList, data, data.isServer)
  return dispatch({
    type: actionTypes.ATTENTION_LIST,
    attentionList: res.data
  })
}
// mci酒店详情内容
export const shareMonitorMci = (data) => async dispatch=> {
  const res = await http.get(urls.share_monitor_mci, data, data.isServer)
  res.msg !== 'OK' && (res.data = null);
  return dispatch({
    type: actionTypes.SHARE_MONITOR_MCI,
    shareMonitorMci: res.data
  })
}
// mci 榜单分享
export const shareRankingMci = (data, isServer) => async dispatch=> {
  const res = await http.get(urls.share_ranking_mci, data, isServer)
  res.msg !== 'OK' && (res.data = null);
  return dispatch({
    type: actionTypes.SHARE_NEWEST_RANKING_MCI,
    shareRankingMci: res.data
  })
}
// mbi 详情分享
export const shareMonitorMbi = (data) => async dispatch=> {
  const url = Number(data.categoryId) === 359 ? urls.share_monitor_tourism_mbi : urls.share_monitor_mbi
  const res = await http.get(url, data, data.isServer)
  res.msg !== 'OK' && (res.data = null);
  return dispatch({
    type: actionTypes.SHARE_MONITOR_MBI,
    shareMonitorMbi: {categoryId: data.categoryId, ...res.data}
  })
}
// mbi 榜单分享
export const shareRankingMbi = (data) => async dispatch=> {
  // urls.share_ranking_tourism_mbi
  const url = Number(data.categoryId) === 359 ? urls.share_ranking_tourism_mbi : urls.share_ranking_mbi
  const res = await http.get(url, data, data.isServer)
  res.msg !== 'OK' && (res.data = null);
  return dispatch({
    type: actionTypes.SHARE_RANKING_MBI,
    shareRankingMbi: res.data
  })
}
// 更多榜单 分享
export const getShareCustomDetail = (data) => async dispatch=> {
  const res = await http.get(urls.share_customDetail, data, data.isServer)
  res.msg !== 'OK' && (res.data = null);
  return dispatch({
    type: actionTypes.SHARE_CUSTOMDETAIL,
    shareCustomDetail: res.data
  })
}
// mci大分类
export const getDivideByType = (data) => async dispatch=> {
  const res = await http.get(urls.divideByType, data, data.isServer);
  res.msg !== 'OK' && (res.data = null);
  return dispatch({
    type: actionTypes.DIVIDEBY_TYPE,
    divideByType: res.data
  })
}
// mci详细分类
export const getMciType = (data) => async dispatch=> {
  const res = await http.get(urls.mci_type, data, data.isServer);
  res.msg !== 'OK' && (res.data = null);
  return dispatch({
    type: actionTypes.MCI_TYPE,
    mci_type: res.data.list
  })
}
// mci详细分类
export const getListRankCustom = (data) => async dispatch=> {
  const res = await http.get(urls.listRankCustom, data, data.isServer);
  !res.ok && (res.data = null);
  return dispatch({
    type: actionTypes.LIST_RANK_CUSTOM,
    listRankCustom: res.data
  })
}
/**
 * mvi榜单请求
 * @param {number} data.type - 类型酒店1107、景区1108
 * @param {number} data.year -
 * @param {number} data.month -
 * @param {number} data.page.currentPage - 页码
 * @param {number} data.page.showCount- 展示条数
 * @param {boolean} data.isServer- 是否服务器请求
 * @returns {object}
 */
export const getMviRank = (data) => async dispatch=> {
  const res = await http.get(urls.getMviRank, data, data.isServer);
  !res.ok && (res.data = null);
  return dispatch({
    type: actionTypes.MVI_RANK,
    mviRank: res.data
  })
}
export const getMviDetail = (data) => async dispatch=> {
  const res = await http.get(urls.getMviDetail, data, data.isServer);
  !res.ok && (res.data = null);
  return dispatch({
    type: actionTypes.MVI_DETAIL,
    mviDetail: res.data
  })
}
export const getTourismDetail = (data) => async dispatch=> {
  const res = await http.get(urls.getTourismDetail, data, data.isServer);
  !res.ok && (res.data = null);
  return dispatch({
    type: actionTypes.TOURISM_DETAIL,
    tourismDetail: res.data
  })
}
export const getTourismMbiIndex = (data) => async dispatch=> {
  const res = await http.get(urls.getTourismMbiIndex, data, data.isServer);
  const indexNum = {
    monitorName: '-',
    policyIndex: '-',
    mediaIndex: '-',
    runningIndex: '-',
    tradeIndex: '-',
    growth: '-',
    ranking: '-',
    defeating: '-',
    gaugeBoard: '-',
    categoryId: '-',
    cityId: '-',
    listRankId: '-',
    mbiIndex: '-',
  }
  !res.ok && (res.data = null);
  if (res.data.length === 0) {
    res.data[0] = indexNum 
  }
  return dispatch({
    type: actionTypes.TOURISM_MBI_INDEX,
    tourismMbiIndex: res.data
  })
}
export const getHistoryBranchRankingTourism = (data) => async dispatch=> {
  const res = await http.get(urls.getHistoryBranchRankingTourism, data, data.isServer);
  !res.ok && (res.data = null);
  return dispatch({
    type: actionTypes.HISTORY_BRANCHRANKING_TOURISM,
    historyBranchRankingTourism: res.data
  })
}
